package org.blueoxygen.kukuruyuk.actions;

import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import org.apache.commons.beanutils.PropertyUtils;
import org.blueoxygen.cimande.LogInformation;
import org.blueoxygen.kukuruyuk.actions.PurchaseOrderForm;
import org.blueoxygen.kukuruyuk.entity.PurchaseOrder;
public class SavePurchaseOrder extends PurchaseOrderForm {
public String execute(){
if(getPurchaseOrder().getSupplier() == null ||
"".equalsIgnoreCase(getPurchaseOrder().getId())){
addActionError("Nama tidak boleh kosong");
}
if(hasErrors()){
return INPUT;
}
LogInformation log;
if(getPurchaseOrder().getId() == null ||
"".equalsIgnoreCase(getPurchaseOrder().getId())){
log = new LogInformation();
log.setCreateBy(sessionCredentials.getCurrentUser().getId());
log.setCreateDate(new Timestamp(System.currentTimeMillis()));
getPurchaseOrder().setId(null);
} else {
PurchaseOrder temp = getPurchaseOrder();
setPurchaseOrder((PurchaseOrder) manager.getById(PurchaseOrder.class, getPurchaseOrder().getId()));
log = getPurchaseOrder().getLogInformation();
try {
PropertyUtils.copyProperties(getPurchaseOrder(), temp);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
log.setLastUpdateBy(sessionCredentials.getCurrentUser().getId());
log.setLastUpdateDate(new Timestamp(System.currentTimeMillis()));
log.setActiveFlag(1);
getPurchaseOrder().setLogInformation(log);
manager.save(getPurchaseOrder());
return SUCCESS;
}
}
